﻿using System;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;
using System.Reflection;
using JigLibX.Physics;
using JigLibX.Collision;
using Services;
using Utilities.Menu.UIElements;
using TrenitaliaWP7.Resources;
using Services.Menu;
using Utilities;
using Microsoft.Phone.Tasks;

namespace TrenitaliaWP7
{
    class CrashMenu : OrientableScreen
    {
        
        ArrayList<MyButton> elements;

        MyButton SendRestart, SendClose, Ignore, DetailsArea;// MailArea;
        InputManager input_manager;
        SpriteBatch spriteBatch;
        StateManager StateManager;
        
        //Texture2D logo;
        Texture2D WhiteTexture;
        SpriteFont font;
        SpriteFont smallfont;
        SpriteFont titleFont;
        //var box = Game.Content.Load<Texture2D>("bar_box");
        Texture2D box;
        //String version;
        String url;
        bool Restart, Close;
        string report;
        public CrashMenu(Game game, string url, string report)
            : base(game)
        {
            this.url = url;
            this.report = report;
        }
        public override void Initialize()
        {
            spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            StateManager = Game.Services.GetService(typeof(AStateManager)) as StateManager;
            Game.Services.RemoveService(typeof(IInputManager));
            input_manager = new InputManager(Game);
            Game.Components.Add(input_manager);
            input_manager.OnTap += on_tap;
            elements = new ArrayList<MyButton>();
            Restart = false;
            Close = false;

            base.Initialize();
        }
        protected override void LoadContent()
        {
            //logo = Game.Content.Load<Texture2D>("Menu/vst_logo");
            //var emptyTexture = Game.Content.Load<Texture2D>("emptyPixel");
            font = Game.Content.Load<SpriteFont>("Fonts/SegoeWP");
            smallfont = Game.Content.Load<SpriteFont>("Fonts/SegoeWP_Small");
            titleFont = Game.Content.Load<SpriteFont>("Fonts/SegoeWP_Title");
            //var box = Game.Content.Load<Texture2D>("bar_box");
            box = Game.Content.Load<Texture2D>("bar_box");
            WhiteTexture = Game.Content.Load<Texture2D>("WhitePixel");
            base.LoadContent();
        }


       
        public override void Update(GameTime gameTime)
        {
            
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                //StateManager.SelectMainMenu();
                StateManager.SelectMainMenu();
                return;
            } 
            base.Update(gameTime);
        }
        Color barColor = new Color(31, 31, 31);
        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            spriteBatch.Begin();
            for (int i = 0; i < elements.Count; i++)
                elements[i].Draw(spriteBatch);
            SendRestart.Draw(spriteBatch);
            SendClose.Draw(spriteBatch);
            Ignore.Draw(spriteBatch);
            DetailsArea.Draw(spriteBatch);
            //MailArea.Draw(spriteBatch);
            spriteBatch.End();
            base.Draw(gameTime);
        }

        void on_tap(Vector2 tap_position)
        {
            if (press_wait < PRESS_INTERVAL) return;
            if (Restart || Close) return;
            if (Ignore.tapped_on(tap_position))
            {
                Thread.Sleep(2000);
                Game.Exit();
                return;
            }
            else if (SendRestart.tapped_on(tap_position))
            {
                Close = false;
                Restart = true;
                SendCrashReport();
                return;
            }
            else if (SendClose.tapped_on(tap_position))
            {
                Restart = false;
                Close = true;
                SendCrashReport();
                return;
            }
            else if (DetailsArea.tapped_on(tap_position))
            {
                //activeButton = DetailsArea;
                //OpenKeyboard(Strings.Bug_Details);

                EmailComposeTask ect = new EmailComposeTask();

                ect.Subject = "Informa Treni - Crash report - " + DateTime.Now.ToString();
                ect.To = "boscariol.francesco@gmail.com";

                ect.Body = "Informa Treni - Crash report - " + DateTime.Now.ToString() + "\n\n" +
                    report + "\n\n\n" + StateManager.BuildCompleteSavedState().ToString();
                ect.Show();
                return;
            }
            //else if (MailArea.tapped_on(tap_position))
            //{
            //    activeButton = MailArea;
            //    OpenKeyboard(Strings.Bug_Email);
            //    return;
            //}
        }

        private void CreateRows(int x, int y, int delta_y, int width, int height, int title_width, int title_height, int button_x, int button_y, int button_width, int button_height, int button_offset)
        {
            elements.Clear();
            elements.Add(new MyButton(new Rectangle(0, 0, title_width, title_height), Strings.Bug_Title, Color.Red, font));
            elements.Add(new MyButton(new Rectangle(x, y, width, height), Strings.Bug_Line1, Color.White, smallfont));
            y += delta_y;
            elements.Add(new MyButton(new Rectangle(x, y, width, height), Strings.Bug_Line2, Color.White, smallfont));
            y += delta_y;
            elements.Add(new MyButton(new Rectangle(x, y, width, height), Strings.Bug_Line3, Color.White, smallfont));
            y += delta_y;
            elements.Add(new MyButton(new Rectangle(x, y, width, height), Strings.Bug_Line4, Color.White, smallfont));
            y += delta_y;
            elements.Add(new MyButton(new Rectangle(x, y, width, height), Strings.Bug_Line5, Color.White, smallfont));


            y += delta_y*2;
            elements.Add(new MyButton(new Rectangle(x, y, width, height), Strings.Bug_Details, Color.White, smallfont));
            y += delta_y;
            DetailsArea = new MyButton(new Rectangle(x, y, width, (int)(height*1.5)), "", Color.Black, smallfont, TextPos.Center, WhiteTexture, Color.White);
            
            
            //y += (int)(delta_y * 1.5);
            
            //elements.Add(new MyButton(new Rectangle(x, y, width, height), Strings.Bug_Email, Color.White, smallfont));
            
            //y += delta_y;
            //MailArea = new MyButton(new Rectangle(x, y, width, (int)(height * 1.5)), "", Color.Black, smallfont, TextPos.Center, WhiteTexture, Color.White);


            SendRestart = new MyButton(new Rectangle(button_x, button_y, button_width, button_height), 
                Strings.Restart, Color.White, smallfont, TextPos.Center, box, Color.White);
            //SendRestart.text2 = Strings.Restart;
            //SendRestart.textColor2 = Color.White;
            //SendRestart.textPos2 = TextPos.Center;
            //SendRestart.font2 = smallfont;
            //SendRestart.textScale2 = 1;
            SendClose = new MyButton(new Rectangle(button_x + button_width + button_offset, button_y, button_width, button_height),
                Strings.Close, Color.White, smallfont, TextPos.Center, box, Color.White);
            //SendClose.text2 = Strings.Close;
            //SendClose.textColor2 = Color.White;
            //SendClose.textPos2 = TextPos.Center;
            //SendClose.font2 = smallfont;
            //SendClose.textScale2 = 1;
            Ignore = new MyButton(new Rectangle(button_x + 2 * (button_width + button_offset), button_y, button_width, button_height),
                Strings.Not_Sent, Color.White, smallfont, TextPos.Center, box, Color.White);
        }

        public override void SetPortraitOrientation()
        {
            CreateRows(40, 100, 50, 400, 40, 480, 100, 20, 700, 120, 80, 40);
        }

       
        public override void SetLandScapeOrientation()
        {
            CreateRows(100, 80, 30, 600, 30, 800, 80, 125, 400, 150, 60, 50);
        }

        void SendCrashReport()
        {
            if (DetailsArea != null && DetailsArea.text != "")
                url += "&dettagli=" + DetailsArea.text;
            //if (MailArea != null && MailArea.text != "")
            //    url += "&mail=" + MailArea.text;
            Utilities.getHTML(url, GotHTML, ErrorHTML);
        }
        void GotHTML(string s)
        {
            if (Close)
                Game.Exit();
            StateManager.SelectMainMenu();
        }
        void ErrorHTML()
        {
            if (Close)
                Game.Exit();
            StateManager.SelectMainMenu();
        }
        //MyButton activeButton;
        //void OpenKeyboard(string title)
        //{
        //    Guide.BeginShowKeyboardInput(
        //           PlayerIndex.One,
        //           title,
        //           "",
        //           activeButton!=null?activeButton.text:"",
        //           new AsyncCallback(OnEndShowKeyboardInput),
        //           null);
        //}
        //private void OnEndShowKeyboardInput(IAsyncResult result)
        //{
        //    if (result != null && Guide.EndShowKeyboardInput(result) != null && activeButton!=null)
        //    {
        //        //list[0].text = Guide.EndShowKeyboardInput(result);

        //        activeButton.text = Guide.EndShowKeyboardInput(result);
        //    }
        //}
    }
}
